package com.honey.solr.schema.node;

import com.honey.compilation.xml.Attribute;
import com.honey.compilation.xml.XmlElement;
import com.honey.solr.SolrFactory;
import com.honey.solr.analyzer.SolrAnalyzerNode;
import com.honey.solr.schema.SchemaNode;

public class Types implements SchemaNode {
	
	private SolrFactory solrFactory;
	
	public Types(SolrFactory solrFactory){
		this.solrFactory = solrFactory;
	}
	
	@Override
	public void appendChildNode(XmlElement root) {
		XmlElement parent = new XmlElement(root,"types");
		baseType(parent);
		
		parent.addNewLine();
		primitiveType(parent);
		
		parent.addNewLine();
		sortableType(parent);
		
		parent.addNewLine();
		generalAnalyzer(parent);
		chineseAnalyzer(parent);
		
		//parent.addNewLine();
		//ignored(parent);
	}
	
	private void generalAnalyzer(XmlElement root){
		SolrAnalyzerNode analyzer = solrFactory.getGeneralAnalyzer();
		if( analyzer != null ){
			analyzer.appendChildNode(root);
		}
	}
	private void chineseAnalyzer(XmlElement root){
		SolrAnalyzerNode analyzer = solrFactory.getChineseAnalyzer();
		if( analyzer != null ){
			analyzer.appendChildNode(root);
		}
	}
	
	private void ignored(XmlElement root){
		new XmlElement(root,"fieldtype")
		.addAttribute("name", "ignored")
		.addAttribute("stored", "false")
		.addAttribute("indexed", "false")
		.addAttribute("multiValued", "true")
		.addAttribute("class", "solr.StrField")
		;
		
	}
	
	/**
	 * 使用 trie建立索引(优化后的索引机制)
	 * @param root
	 */
	private void baseType(XmlElement root ){
		Attribute precisionStep0 = new Attribute("precisionStep","0");
		Attribute precisionStep8 = new Attribute("precisionStep","8");
		
		Attribute positionIncrementGap = new Attribute("positionIncrementGap","0");
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","string")
		.addAttribute("class", "solr.StrField")
		.addAttribute("sortMissingLast", "true")
		.addAttribute("omitNorms", "true")
		;
//		new XmlElement(root,"fieldType")
//		.addAttribute("name","char")
//		.addAttribute("class", "solr.StrField")
//		.addAttribute("sortMissingLast", "true")
//		.addAttribute("omitNorms", "true")
//		;
		new XmlElement(root,"fieldType")
		.addAttribute("name","boolean")
		.addAttribute("class", "solr.BoolField")
		.addAttribute("sortMissingLast", "true")
		.addAttribute("omitNorms", "true")
		;
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","binary")
		.addAttribute("class", "solr.BinaryField")
		.addAttribute("sortMissingLast", "true")
		.addAttribute("omitNorms", "true")
		;

//		new XmlElement(root,"fieldType")
//		.addAttribute("name","integer")
//		.addAttribute("class", "solr.IntField")
//		.addAttribute("sortMissingLast", "true")
//		.addAttribute("omitNorms", "true")
//		;
		
		root.addNewLine();
		root.addComments("使用trie树建立索引,是一种比较优化的索引类型 ");
		new XmlElement(root,"fieldType")
		.addAttribute("name","int")
		.addAttribute("class", "solr.TrieIntField")
		.addAttribute(precisionStep0)
		.addAttribute(positionIncrementGap)
		;
//		
//		new XmlElement(root,"fieldType")
//		.addAttribute("name","short")
//		.addAttribute("class", "solr.TrieIntField")
//		.addAttribute(precisionStep0)
//		.addAttribute(positionIncrementGap)
//		;
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","long")
		.addAttribute("class", "solr.TrieLongField")
		.addAttribute(precisionStep0)
		.addAttribute(positionIncrementGap)
		;

		new XmlElement(root,"fieldType")
		.addAttribute("name","float")
		.addAttribute("class", "solr.TrieFloatField")
		.addAttribute(precisionStep0)
		.addAttribute(positionIncrementGap)
		;

		new XmlElement(root,"fieldType")
		.addAttribute("name","double")
		.addAttribute("class", "solr.TrieDoubleField")
		.addAttribute(precisionStep0)
		.addAttribute(positionIncrementGap)
		;
		
		//----------------
		root.addNewLine();
		new XmlElement(root,"fieldType")
		.addAttribute("name","tint")
		.addAttribute("class", "solr.TrieIntField")
		.addAttribute(precisionStep8)
		.addAttribute(positionIncrementGap)
		;
//		
//		new XmlElement(root,"fieldType")
//		.addAttribute("name","tshort")
//		.addAttribute("class", "solr.TrieIntField")
//		.addAttribute(precisionStep8)
//		.addAttribute(positionIncrementGap)
//		;
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","tlong")
		.addAttribute("class", "solr.TrieLongField")
		.addAttribute(precisionStep8)
		.addAttribute(positionIncrementGap)
		;

		new XmlElement(root,"fieldType")
		.addAttribute("name","tfloat")
		.addAttribute("class", "solr.TrieFloatField")
		.addAttribute(precisionStep8)
		.addAttribute(positionIncrementGap)
		;

		new XmlElement(root,"fieldType")
		.addAttribute("name","tdouble")
		.addAttribute("class", "solr.TrieDoubleField")
		.addAttribute(precisionStep8)
		.addAttribute(positionIncrementGap)
		;
		
		root.addNewLine();
		new XmlElement(root,"fieldType")
		.addAttribute("name","date")
		.addAttribute("class", "solr.TrieDateField")
		.addAttribute(precisionStep0)
		.addAttribute(positionIncrementGap)
		;
		new XmlElement(root,"fieldType")
		.addAttribute("name","tdate")
		.addAttribute("class", "solr.TrieDateField")
		.addAttribute(new Attribute("precisionStep","6"))
		.addAttribute(positionIncrementGap)
		;

	}
	
	/**
	 * 原始类型
	 * @param root
	 */
	private void primitiveType(XmlElement root){
		
		root.addComments("原始类型,索引结构没有任何优化.");
		new XmlElement(root,"fieldType")
		.addAttribute("name","pint")
		.addAttribute("class", "solr.IntField")
		;
		
//		new XmlElement(root,"fieldType")
//		.addAttribute("name","pshort")
//		.addAttribute("class", "solr.IntField")
//		;
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","plong")
		.addAttribute("class", "solr.LongField")
		;

		new XmlElement(root,"fieldType")
		.addAttribute("name","pfloat")
		.addAttribute("class", "solr.FloatField")
		;

		new XmlElement(root,"fieldType")
		.addAttribute("name","pdouble")
		.addAttribute("class", "solr.DoubleField")
		;
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","pdate")
		.addAttribute("class", "solr.DateField")
		;
		
	}
	
	//低版本的solr索引类型,抛弃使用
	private void sortableType(XmlElement root ){
		root.addComments("排序类型.");
		new XmlElement(root,"fieldType")
		.addAttribute("name","sint")
		.addAttribute("class", "solr.SortableIntField")
		.addAttribute("sortMissingLast", "true")
		.addAttribute("omitNorms", "true")
		;
//		new XmlElement(root,"fieldType")
//		.addAttribute("name","sshort")
//		.addAttribute("class", "solr.SortableIntField")
//		.addAttribute("sortMissingLast", "true")
//		.addAttribute("omitNorms", "true")
//		;
//		
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","slong")
		.addAttribute("class", "solr.SortableLongField")
		.addAttribute("sortMissingLast", "true")
		.addAttribute("omitNorms", "true")
		;

		new XmlElement(root,"fieldType")
		.addAttribute("name","sfloat")
		.addAttribute("class", "solr.SortableFloatField")
		.addAttribute("sortMissingLast", "true")
		.addAttribute("omitNorms", "true")
		;
		
		new XmlElement(root,"fieldType")
		.addAttribute("name","sdouble")
		.addAttribute("class", "solr.SortableDoubleField")
		.addAttribute("sortMissingLast", "true")
		.addAttribute("omitNorms", "true")
		;
	}
}
